﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ColorClusteringSOM
{
    [Serializable]
    public class Network
    {
        protected int inputNeurons = 0;
        protected int outputNeurons = 0;
        protected double normalizationFactor = 0;

        // Toto je neuronova sit
        private Neuron[,] neuronsOutputMatrix = null;


        private int rows = -1;
        private int cols = -1;


        #region Properties
        public int Rows
        {
            get { return this.rows; }

        }

        public int Cols
        {
            get { return this.cols; }

        }

        protected Neuron[,] NeuronsOutputMatrix
        {
            get { return neuronsOutputMatrix; }

        }

        /// <summary>
        /// Primy pristup k poli site
        /// </summary>
        /// <param name="index"></param>
        /// <param name="indey"></param>
        /// <returns></returns>
        public Neuron this[int index, int indey]
        {
            get { return neuronsOutputMatrix[index, indey]; }
        }

        public int InputNeurons { get{return this.inputNeurons;} }

        #endregion

        /// <summary>
        /**
         * Vytvori sit na zaklade poctu vystupnich
         * neuronu. Zatim jen ctvercovou. tzn. ze bude
         * o velikosti outputneurons x outputneurons
         * 
         * vola se metoda FillNetworkRandom pro nastaveni
         * nahodny hodnot vah neuronu
         * 
         */
        /// </summary>
        /// <param name="inputNeurons"></param>
        /// <param name="outputNeurons"></param>
        public Network(int inputNeurons, int outputNeurons)
        {
            this.inputNeurons = inputNeurons;
            this.outputNeurons = outputNeurons;
            this.rows = outputNeurons;
            this.cols = outputNeurons;

            neuronsOutputMatrix = new Neuron[this.rows, this.cols];

            Console.WriteLine("Sit vytvorena. Radky = " + rows + " Sloupce = " + cols);

            FillNetworkRandom();
            // PrintNetworkValues();


        }


        /// <summary>
        /// Nastavi nahodne vahy neuronu v siti a jejich pozici [X,Y]
        /// </summary>
        private void FillNetworkRandom()
        {
            Console.WriteLine("Nastaveni nahodnych hondot vah vsem neuronum v siti...");
            Neuron n = null;
            int pomNum = 0;
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    n = new Neuron(this.inputNeurons);
                    n.RandomizeWeights();
                    n.X = i;
                    n.Y = j;
                    n.NumberOfNeuron = pomNum;
                    neuronsOutputMatrix[i, j] = n;
                    n = null;
                    pomNum++;
                }
                
            }
        }

        public void PrintNetworkValues()
        {
            for (int i = 0; i < cols; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < rows; j++)
                {
                    Console.Write(" --- " + this.neuronsOutputMatrix[i, j].ToString());
                }
            }
        }



    }
}
